﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using System.Windows.Forms;
using System.Xml;


using DotSpatial.Controls;

using DotSpatial.Serialization;





namespace HaGisPack
{    
   public  class GisManager:IProj
   {


       public GisManager(Map m)
       {
           m_Map = m;

           //创建特有的运行环境以及插件环境

           //初始化所有子系统

           //初始化核心子系统
           m_LayerSys=new LayerSystem(this);
         


           //初始化辅助系统
           m_MFSys = new MFSystem(this);

           m_UISys = new UISystem(this);
            




       }


       #region 表现层相关的属性

       Map m_Map;
       /// <summary>
       /// 返回当前的图形接口
       /// </summary>
       public Map CurrentMap
       {
           get { return m_Map; }
       }



      


       #endregion


       #region 主体特有系统

       UISystem m_UISys;

       public UISystem UISys
       {
           get { return m_UISys; }
           
       }



       MFSystem m_MFSys;

       public MFSystem MFSys
       {
           get { return m_MFSys; }
          
       }
      
       LayerSystem m_LayerSys;


       /// <summary>
       /// 主要的层管理器
       /// </summary>
       public LayerSystem LayerSys
       {
           get { return this.m_LayerSys; }
       }
       

       #endregion


       //#region 对外部的接口

       ////项目相关的接口部分




       //#endregion



       #region IProj 成员

       /// <summary>
       /// 不包含路径的主文件保存文件名
       /// </summary>
       string m_projName="main.gis";

       string m_mapXml = "main.map.xml";

       /// <summary>
       /// 实际的工作目录
       /// </summary>
       string m_workPath;

       /// <summary>
       /// 程序主保存文件的地址和文件名
       /// </summary>
       public string AbsProjPathName
       {
           get { return this.m_workPath + m_projName; }
 
       }

       public string AbsMapPathName
       {
           get { return this.m_workPath + m_mapXml; }
       }

    

       /// <summary>
       /// 工作目录，形式为c:\test\projpath\gis
       /// </summary>
       public string workPath
       {
           get { return this.m_workPath; }
          
 
       }

       public string layerPath
       {
           get {
               return this.m_workPath + "layers\\";
           }
       }

       public bool SaveProj()
       {

           //try
           //{
           //    XmlSerializer s = new XmlSerializer();
           //    string xml = s.Serialize(this.m_Map );
           //    File.WriteAllText(this.AbsMapPathName  , xml);
           //}
           //catch (XmlException)
           //{

           //    return false;

           //}
           //catch (IOException)
           //{
           //    return false;

           //}


           return m_LayerSys.SaveProj();
       }

       public bool OpenProj(string wPath)
       {

           this.m_workPath = wPath + "gis\\";
           
           
         

           return m_LayerSys.OpenProj();

       }


       /// <summary>
       /// 打开一个工作路径
       /// </summary>
       /// <param name="wPath">路径地址，形式为c;\proj\</param>
       /// <returns></returns>
       public bool NewProj(string wPath)
       {

           this.m_workPath = wPath+"gis\\";

           if (!Directory .Exists (this.m_workPath ))
           {

               Directory.CreateDirectory(this.m_workPath);

           }
           else
           {


               ///如果当前存在目录，并且目录不为空，需要提示用户是否继续
               ///但是由于这个是组件程序，询问的功能是主程要做的，我总是混合两者的概念，所以这里不需要问了，直接覆盖
               Directory.Delete(this.m_workPath,true  );

               Directory.CreateDirectory(this.m_workPath);

           }


           if (!Directory .Exists (this.layerPath ))
           {
               Directory.CreateDirectory(layerPath );
               
           }



           this.m_LayerSys.NewProj();

           return this.SaveProj();


       }

       public bool CloseProj()
       {

           this.m_workPath = "";
           return m_LayerSys.CloseProj();
 
       }


       /// <summary>
       /// 暂时不会实现这个函数
       /// </summary>
       /// <param name="wPath"></param>
       /// <returns></returns>
       public bool SaveAsProj(string wPath)
       {
           return m_LayerSys.SaveAsProj();
       }

       #endregion
   }
}
